{
 "metadata": {
  "signature": "sha256:90a3be583b0039cc4586a68e231cd29d5ece833f121e217095554e90d6a36776"
 },
 "nbformat": 3,
 "nbformat_minor": 0,
 "worksheets": [
  {
   "cells": [
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Matplotlib: multiple subplots with one axis label\n",
      "======================================================================\n",
      "\n",
      "Using a single axis label to annotate multiple subplot axes\n",
      "-----------------------------------------------------------\n",
      "\n",
      "When using multiple subplots with the same axis units, it is redundant\n",
      "to label each axis individually, and makes the graph overly complex. You\n",
      "can use a single axis label, centered in the plot frame, to label\n",
      "multiple subplot axes. Here is how to do it:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "# note that this a code fragment...you will have to define your own data to plot\n",
      "# Set up a whole-figure axes, with invisible axis, ticks, and ticklabels,\n",
      "# which we use to get the xlabel and ylabel in the right place\n",
      "bigAxes = pylab.axes(frameon=False)     # hide frame\n",
      "pylab.xticks([])                        # don't want to see any ticks on this axis\n",
      "pylab.yticks([])\n",
      "# I'm using TeX for typesetting the labels--not necessary\n",
      "pylab.ylabel(r'\\textbf{Surface Concentration $(nmol/m^2)$}', size='medium')\n",
      "pylab.xlabel(r'\\textbf{Time (hours)}', size='medium')\n",
      "# Create subplots and shift them up and to the right to keep tick labels\n",
      "# from overlapping the axis labels defined above\n",
      "topSubplot = pylab.subplot(2,1,1)\n",
      "position = topSubplot.get_position()\n",
      "position[0] = 0.15\n",
      "position[1] = position[1] + 0.01\n",
      "topSubplot.set_position(position)\n",
      "pylab.errorbar(times150, average150)\n",
      "bottomSubplot = pylab.subplot(2,1,2)\n",
      "position = bottomSubplot.get_position()\n",
      "position[0] = 0.15\n",
      "position[1] = position[1] + 0.03\n",
      "bottomSubplot.set_position(position)\n",
      "pylab.errorbar(times300, average300)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "1.  1.  \n",
      "\n",
      "Alternatively, you can use the following snippet to have shared ylabels\n",
      "on your subplots. Also see the attached [figure\n",
      "output](![](files/attachments/Matplotlib_Multiple_Subplots_with_One_Axis_Label/Same_ylabel_subplots.png).\n",
      ")# "
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "import pylab\n",
      "\n",
      "figprops = dict(figsize=(8., 8. / 1.618), dpi=128)                                          # Figure properties\n",
      "adjustprops = dict(left=0.1, bottom=0.1, right=0.97, top=0.93, wspace=0.2 hspace=0.2)       # Subplot properties\n",
      "\n",
      "fig = pylab.figure(**figprops)                                                              # New figure\n",
      "fig.subplots_adjust(**adjustprops)                                                          # Tunes the subplot layout\n",
      "\n",
      "ax = fig.add_subplot(3, 1, 1)\n",
      "bx = fig.add_subplot(3, 1, 2, sharex=ax, sharey=ax)\n",
      "cx = fig.add_subplot(3, 1, 3, sharex=ax, sharey=ax)\n",
      "\n",
      "ax.plot([0,1,2], [2,3,4], 'k-')\n",
      "bx.plot([0,1,2], [2,3,4], 'k-')\n",
      "cx.plot([0,1,2], [2,3,4], 'k-')\n",
      "\n",
      "pylab.setp(ax.get_xticklabels(), visible=False)\n",
      "pylab.setp(bx.get_xticklabels(), visible=False)\n",
      "\n",
      "bx.set_ylabel('This is a long label shared among more axes', fontsize=14)\n",
      "cx.set_xlabel('And a shared x label', fontsize=14)"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "Thanks to Sebastian Krieger from matplotlib-users list for this trick.\n",
      "\n",
      "1.  1.  \n",
      "\n",
      "1.  1.  \n",
      "\n",
      "Simple function to get rid of superfluous xticks but retain the ones on\n",
      "the bottom (works in pylab). Combine it with the above snippets to get a\n",
      "nice plot without too much redundance:"
     ]
    },
    {
     "cell_type": "code",
     "collapsed": false,
     "input": [
      "#!python\n",
      "def rem_x():\n",
      "    '''Removes superfluous x ticks when multiple subplots  share\n",
      "    their axis works only in pylab mode but can easily be rewritten\n",
      "    for api use'''\n",
      "    nr_ax=len(gcf().get_axes())\n",
      "    count=0\n",
      "    for z in gcf().get_axes():\n",
      "        if count == nr_ax-1: break\n",
      "            setp(z.get_xticklabels(),visible=False)\n",
      "            count+=1"
     ],
     "language": "python",
     "metadata": {},
     "outputs": []
    },
    {
     "cell_type": "markdown",
     "metadata": {},
     "source": [
      "The first one above doesn't work for me. The subplot command overwrites the bigaxes.\n",
      "However, I found a much simpler solution to do a decent job for two axes and one ylabel:\n",
      "\n",
      "yyl=plt.ylabel(r'My longish label that I want vertically centred')\n",
      "\n",
      "yyl.set_position((yyl.get_position()[0],1)) # This says use the top of the bottom axis as the reference point.\n",
      "\n",
      "yyl.set_verticalalignment('center')"
     ]
    }
   ],
   "metadata": {}
  }
 ]
}